package com.xrui.hbase.schema.impl;

import com.google.common.base.Objects;

import com.xrui.hbase.conversion.AvroCellConverter;
import com.xrui.hbase.schema.SchemaType;
import org.apache.avro.Schema;

public final class AvroCellSchema extends GenericCellSchema {
    private String schemaID;

    private AvroCellSchema(String id, String value, String compression, String converter) {
        super(SchemaType.AVRO, value, compression, converter);
        schemaID = id;
    }

    public static AvroCellSchema create(String id, String value, String compression, String converter) {
        if (null == converter || converter.isEmpty()) {
            return new AvroCellSchema(id, value, compression, AvroCellConverter.PROVIDER_NAME);
        } else {
            return new AvroCellSchema(id, value, compression, converter);
        }
    }

    public static AvroCellSchema create(String id, String value, String compression) {
        return create(id, value, compression, null);
    }

    public static AvroCellSchema create(String id, String value) {
        return create(id, value, null, null);
    }

    public Schema getSchema() {
        return new Schema.Parser().parse(this.getValue());
    }

    public String getSchemaJson() {
        return getValue();
    }

    public String getSchemaID() {
        return schemaID;
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(AvroCellSchema.class)
            .add("type", mSchemaType)
            .add("id", schemaID)
            .add("schema", mValue)
            .add("converter", mConverter)
            .add("compression", mCompressionProvider)
            .toString();
    }
}
